--[[

ExpTables = {}

--Checks that the table exists / creates it
function ExpTables:CheckTable()
	if not sql.TableExists( "player_exp" ) then
		local query = "CREATE TABLE player_exp ( unique_id varchar(20), exp int )" --You don't need 255 chars of space. SteamIDs only have 16 characters or so. 20 will suffice.
		local result = sql.Query( query )
		if sql.TableExists( "player_exp" ) then
			print( "Table created." )
		else
			print( "Table couldn't be created. Error: " .. sql.LastError() )
		end
	end
end

--Sets the NWInts on the player for XP
function ExpTables:SetNetworkedXP( ply )
	local xp = sql.QueryValue( "SELECT exp FROM player_exp WHERE unique_id = '" .. ply:SteamID() .. "'" )
	ply:SetNWInt ( "exp", xp )
end

function ExpTables:SaveXP( ply )
	local xp = ply:GetNWInt( "exp" )
	sql.Query("UPDATE player_exp SET exp = " .. xp .." WHERE unique_id = '" .. ply:SteamID() .. "'")
	
	ply:ChatPrint( "Your experience has been saved." )
end

function ExpTables:AddPlayer( ply )
	local SteamID = ply:SteamID()
	sql.Query( "INSERT INTO player_exp VALUES ('" .. SteamID .. "', '0' )" )
	
	local exists = self:PlayerExists( ply )
	
	if exists then 
		ply:ChatPrint( "Your account has been created!" )
		self:SetNetworkedXP( ply )
		return true
	else
		ply:ChatPrint( "Your account couldn't be created. :(" )
		return false
	end
end
 
function ExpTables:CheckPlayer( ply )
	if self:PlayerExists( ply ) then
		--We're fine, just update their XP
		self:SetNetworkedXP( ply )
	else
		if self:AddPlayer( ply ) then
			print( ply:Name() .. "'s account was created :D" )
		else
			ErrorNoHalt( "[Warning] " .. ply:Name() .. "'s account couldn't be created." )
		end
	end
end
 
function ExpTables:PlayerExists( ply )
	local result = sql.QueryValue( "SELECT unique_id FROM player_exp WHERE unique_id = '" .. ply:SteamID() .. "'")
	
	return result ~= ""
end

local function Initialize()
	ExpTables:CheckTable()
end

local function PlayerInitialSpawn( ply )
	timer.Create("PlayerCheck", 10, 0, function( ply )
		ExpTables:CheckPlayer( ply )
	end, ply)
end

hook.Add( "PlayerInitialSpawn", "PlayerInitialSpawn", PlayerInitialSpawn )
hook.Add( "Initialize", "Initialize", Initialize )

function AddEXP()
	for k,v in pairs(player.GetAll()) do
		v:SetNWInt( "exp", v:GetNWInt( "exp" ) + 100 )
		v:SendLua("GAMEMODE:AddNotify(\"You have gained experience!\", NOTIFY_GENERIC, 4)")
	end
end

timer.Create( "AddEXP", 120, 0, AddEXP )--]]